jQuery(document).ready(function () {
    //页面初始化
    formInit();
    //事件注册
    eventInit();
});

//初始化
function formInit() {
    if (editData) {
        renderFormData(".layui-form", editData);
        //渲染账户信息，暂时只取一个
        if(isNotEmpty(editData.hcChrgClientAccountList) && editData.hcChrgClientAccountList.length > 0){
            $("#acName").val(editData.hcChrgClientAccountList[0].acName);
            $("#acBank").val(editData.hcChrgClientAccountList[0].acBank);
            $("#acNo").val(editData.hcChrgClientAccountList[0].acNo);
        }
        //渲染附件
        if (isNotEmpty(editData.hcChrgClientAppendList) && editData.hcChrgClientAppendList.length > 0) {
            let clientYYZZHtml = "";
            let clientFRHtml = "";
            let clientJSXHtml = "";
            let clientWTSHtml = "";
            let clientYJXYHtml = "";
            $.each(editData.hcChrgClientAppendList, function (i, item) {
                if('14' == item.type){
                    clientYYZZHtml += `<li class='list-group-item mCustomScrollBox oldFileClass'><span class='clientYYZZbadge' title='${item.id}'>下载</span><span class='view' type="${item.atype}" title='${item.axdpath}'>预览</span>${item.aname}</li>`;
                }
                if('16' == item.type){
                    clientFRHtml += `<li class='list-group-item mCustomScrollBox oldFileClass'><span class='clientFRbadge' title='${item.id}'>下载</span><span class='view' type="${item.atype}" title='${item.axdpath}'>预览</span>${item.aname}</li>`;
                }
                if('19' == item.type){
                    clientJSXHtml += `<li class='list-group-item mCustomScrollBox oldFileClass'><span class='clientJSXbadge' title='${item.id}'>下载</span><span class='view' type="${item.atype}" title='${item.axdpath}'>预览</span>${item.aname}</li>`;
                }
                if('21' == item.type){
                    clientWTSHtml += `<li class='list-group-item mCustomScrollBox oldFileClass'><span class='clientWTSbadge' title='${item.id}'>下载</span><span class='view' type="${item.atype}" title='${item.axdpath}'>预览</span>${item.aname}</li>`;
                }
                if('22' == item.type){
                    clientYJXYHtml += `<li class='list-group-item mCustomScrollBox oldFileClass'><span class='clientYJXYbadge' title='${item.id}'>下载</span><span class='view' type="${item.atype}" title='${item.axdpath}'>预览</span>${item.aname}</li>`;
                }
            });
            $('#historyClientYYZZFileDiv>ul').empty().append(clientYYZZHtml);
            $('#historyClientFRFileDiv>ul').empty().append(clientFRHtml);
            $('#historyClientJSXFileDiv>ul').empty().append(clientJSXHtml);
            $('#historyClientWTSFileDiv>ul').empty().append(clientWTSHtml);
            $('#historyClientYJXYFileDiv>ul').empty().append(clientYJXYHtml);
        }
    }
}

//加载事件
function eventInit() {
    //返回
    $("#cancel_back").on("click", function () {
        parent.layer.closeAll();
        parent.clientTable.reload();
    });

    //下载
    $('.clientYYZZbadge').on('click', function () {
        let title = $(this).attr('title');
        location.href = `${basePath}/hcChrgClientAppend/downLoad/${title}`;
    });
    $('.clientFRbadge').on('click', function () {
        let title = $(this).attr('title');
        location.href = `${basePath}/hcChrgClientAppend/downLoad/${title}`;
    });
    $('.clientJSXbadge').on('click', function () {
        let title = $(this).attr('title');
        location.href = `${basePath}/hcChrgClientAppend/downLoad/${title}`;
    });
    $('.clientWTSbadge').on('click', function () {
        let title = $(this).attr('title');
        location.href = `${basePath}/hcChrgClientAppend/downLoad/${title}`;
    });
    $('.clientYJXYbadge').on('click', function () {
        let title = $(this).attr('title');
        location.href = `${basePath}/hcChrgClientAppend/downLoad/${title}`;
    });

    //预览
    $('.view').on('click', function () {
        //获取文件的相对路劲，需要配置虚拟路径basePath
        window.open("/filePath/" + $(this).attr('title'));
        /*let url = "/filePath/" + $(this).attr('title');//相对路径
        let setting = {
            type: 1,
            title: false,
            closeBtn: 0,
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: "<img src='" + url + "'/>",
            shade: 0.6 //遮罩透明度
        };
        let windowH = $(window).height();
        let windowW = $(window).width();
        getImageWidth(url,function(w,h ) {
            // 调整图片大小
            if (w > windowW * 0.85 || h > windowH * 0.95) {
                if (w > windowW * 0.85 && h > windowH * 0.95) {
                    setting.area = [windowW * 0.85 + "px", windowH * 0.95 + "px"];
                } else if (w > windowW * 0.85) {
                    setting.area = [windowW * 0.85 + "px", h + "px"];
                } else {
                    setting.area = [w + "px", windowH * 0.95 + "px"];
                }
            } else {
                setting.area = [w + "px", h + "px"];
            }
            // 设置layer
            layer.open(setting);
        });*/
    });
}

//预览
function getImageWidth(url,callback){
    let img = new Image();
    img.src = url;
    // 如果图片被缓存，则直接返回缓存数据
    if(img.complete){
        callback(img.width, img.height);
    }else{
        // 完全加载完毕的事件
        img.onload = function(){
            callback(img.width, img.height);
        }
    }
}
